Skip to content

Conversation

@guan404ming
Copy link
Member

Why

The MSSQL THROW statement was missing from the sqlparser, preventing parsing of T-SQL error handling code.

How

  • Add THROW keyword and Statement::Throw AST variant with optional error_number, message, state
  • Add parse_throw() handling both THROW (re-throw) and THROW num, msg, state forms
  • Add tests for literal args, variable args, and argument-less re-throw

Signed-off-by: Guan-Ming (Wesley) Chiu <105915352+guan404ming@users.noreply.github.com>
@guan404ming
Copy link
Member Author

Hi @iffyio I would like modify the PR title to MSSQL: support THROW statement to better align the convention here but I found there is no edit button here for this pr. Could you help modify it, thanks!

@iffyio iffyio changed the title Add MSSQL THROW statement support MSSQL: Support THROW statement Feb 7, 2026
@iffyio
Copy link
Contributor

iffyio commented Feb 7, 2026

Done!

@guan404ming
Copy link
Member Author

Thanks! Please let me know if there is anything could be better in this pr, thanks!

src/ast/mod.rs Outdated
Comment on lines 4684 to 4691
Throw {
/// Error number expression.
error_number: Option<Box<Expr>>,
/// Error message expression.
message: Option<Box<Expr>>,
/// State expression.
state: Option<Box<Expr>>,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're slowly moving away from this representation over to having all statements wrapped in a named struct. As a result, can we do something like this instead?

struct Throw { ... }
Statement::Throw(Throw)

See the RAISE statement for an example

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your information. I just update with wrapped named struct.

Keyword::RELEASE => self.parse_release(),
Keyword::COMMIT => self.parse_commit(),
Keyword::RAISERROR => Ok(self.parse_raiserror()?),
Keyword::THROW => Ok(self.parse_throw()?),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Keyword::THROW => Ok(self.parse_throw()?),
Keyword::THROW => {
self.prev_token();
self.parse_throw().map(Into::into)
},

see RAISE for an example. we're moving to have the statement parsing functions return the actual struct instead of a Statement enum variant. Also it would be ideal that the parse_throw is a standalone function (able to parse a THROW statement) so we rewind the token before invoking it

Comment on lines 18268 to 18276
if self.peek_token_ref().token == Token::SemiColon
|| self.peek_token_ref().token == Token::EOF
{
return Ok(Statement::Throw {
error_number: None,
message: None,
state: None,
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if self.peek_token_ref().token == Token::SemiColon
|| self.peek_token_ref().token == Token::EOF
{
return Ok(Statement::Throw {
error_number: None,
message: None,
state: None,
});
}

I think we can skip this logic, it would be expected for the function to return an error if the input is empty (per the previous comment about making this function stand-alone)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that makes sense to me. Just moved~

Co-Authored-By: Ifeanyi Ubah <7816405+iffyio@users.noreply.github.com>
@guan404ming guan404ming force-pushed the feat/mssql-throw-statement branch from f184eff to 4c18f7a Compare February 9, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants